home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 March / maximum-cd-2000-03.iso / Quake3 Game Source / Q3AGameSource.exe / Main / ui_confirm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-18  |  4.0 KB  |  187 lines

  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3. /*
  4. =======================================================================
  5.  
  6. CONFIRMATION MENU
  7.  
  8. =======================================================================
  9. */
  10.  
  11.  
  12. #include "ui_local.h"
  13.  
  14.  
  15. #define ART_CONFIRM_FRAME    "menu/art/cut_frame"
  16.  
  17. #define ID_CONFIRM_NO        10
  18. #define ID_CONFIRM_YES        11
  19.  
  20.  
  21. typedef struct {
  22.     menuframework_s menu;
  23.  
  24.     menutext_s        no;
  25.     menutext_s        yes;
  26.  
  27.     int                slashX;
  28.     const char *    question;
  29.     void            (*draw)( void );
  30.     void            (*action)( qboolean result );
  31. } confirmMenu_t;
  32.  
  33.  
  34. static confirmMenu_t    s_confirm;
  35.  
  36.  
  37. /*
  38. =================
  39. ConfirmMenu_Event
  40. =================
  41. */
  42. static void ConfirmMenu_Event( void* ptr, int event ) {
  43.     qboolean    result;
  44.  
  45.     if( event != QM_ACTIVATED ) {
  46.         return;
  47.     }
  48.  
  49.     UI_PopMenu();
  50.  
  51.     if( ((menucommon_s*)ptr)->id == ID_CONFIRM_NO ) {
  52.         result = qfalse;
  53.     }
  54.     else {
  55.         result = qtrue;
  56.     }
  57.  
  58.     if( s_confirm.action ) {
  59.         s_confirm.action( result );
  60.     }
  61. }
  62.  
  63.  
  64. /*
  65. =================
  66. ConfirmMenu_Key
  67. =================
  68. */
  69. static sfxHandle_t ConfirmMenu_Key( int key ) {
  70.     switch ( key ) {
  71.     case K_KP_LEFTARROW:
  72.     case K_LEFTARROW:
  73.     case K_KP_RIGHTARROW:
  74.     case K_RIGHTARROW:
  75.         key = K_TAB;
  76.         break;
  77.  
  78.     case 'n':
  79.     case 'N':
  80.         ConfirmMenu_Event( &s_confirm.no, QM_ACTIVATED );
  81.         break;
  82.  
  83.     case 'y':
  84.     case 'Y':
  85.         ConfirmMenu_Event( &s_confirm.yes, QM_ACTIVATED );
  86.         break;
  87.     }
  88.  
  89.     return Menu_DefaultKey( &s_confirm.menu, key );
  90. }
  91.  
  92.  
  93. /*
  94. =================
  95. ConfirmMenu_Draw
  96. =================
  97. */
  98. static void ConfirmMenu_Draw( void ) {
  99.     UI_DrawNamedPic( 142, 118, 359, 256, ART_CONFIRM_FRAME );
  100.     UI_DrawProportionalString( 320, 204, s_confirm.question, UI_CENTER|UI_INVERSE, color_red );
  101.     UI_DrawProportionalString( s_confirm.slashX, 265, "/", UI_LEFT|UI_INVERSE, color_red );
  102.  
  103.     Menu_Draw( &s_confirm.menu );
  104.  
  105.     if( s_confirm.draw ) {
  106.         s_confirm.draw();
  107.     }
  108. }
  109.  
  110.  
  111. /*
  112. =================
  113. ConfirmMenu_Cache
  114. =================
  115. */
  116. void ConfirmMenu_Cache( void ) {
  117.     trap_R_RegisterShaderNoMip( ART_CONFIRM_FRAME );
  118. }
  119.  
  120.  
  121. /*
  122. =================
  123. UI_ConfirmMenu
  124. =================
  125. */
  126. void UI_ConfirmMenu( const char *question, void (*draw)( void ), void (*action)( qboolean result ) ) {
  127.     uiClientState_t    cstate;
  128.     int    n1, n2, n3;
  129.     int    l1, l2, l3;
  130.  
  131.     // zero set all our globals
  132.     memset( &s_confirm, 0, sizeof(s_confirm) );
  133.  
  134.     ConfirmMenu_Cache();
  135.  
  136.     n1 = UI_ProportionalStringWidth( "YES/NO" );
  137.     n2 = UI_ProportionalStringWidth( "YES" ) + PROP_GAP_WIDTH;
  138.     n3 = UI_ProportionalStringWidth( "/" )  + PROP_GAP_WIDTH;
  139.     l1 = 320 - ( n1 / 2 );
  140.     l2 = l1 + n2;
  141.     l3 = l2 + n3;
  142.     s_confirm.slashX = l2;
  143.  
  144.     s_confirm.question = question;
  145.     s_confirm.draw = draw;
  146.     s_confirm.action = action;
  147.  
  148.     s_confirm.menu.draw       = ConfirmMenu_Draw;
  149.     s_confirm.menu.key        = ConfirmMenu_Key;
  150.     s_confirm.menu.wrapAround = qtrue;
  151.  
  152.     trap_GetClientState( &cstate );
  153.     if ( cstate.connState >= CA_CONNECTED ) {
  154.         s_confirm.menu.fullscreen = qfalse;
  155.     }
  156.     else {
  157.         s_confirm.menu.fullscreen = qtrue;
  158.     }
  159.  
  160.     s_confirm.yes.generic.type        = MTYPE_PTEXT;      
  161.     s_confirm.yes.generic.flags        = QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS; 
  162.     s_confirm.yes.generic.callback    = ConfirmMenu_Event;
  163.     s_confirm.yes.generic.id        = ID_CONFIRM_YES;
  164.     s_confirm.yes.generic.x            = l1;
  165.     s_confirm.yes.generic.y            = 264;
  166.     s_confirm.yes.string            = "YES";
  167.     s_confirm.yes.color                = color_red;
  168.     s_confirm.yes.style                = UI_LEFT;
  169.  
  170.     s_confirm.no.generic.type        = MTYPE_PTEXT;      
  171.     s_confirm.no.generic.flags        = QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS; 
  172.     s_confirm.no.generic.callback    = ConfirmMenu_Event;
  173.     s_confirm.no.generic.id            = ID_CONFIRM_NO;
  174.     s_confirm.no.generic.x            = l3;
  175.     s_confirm.no.generic.y            = 264;
  176.     s_confirm.no.string                = "NO";
  177.     s_confirm.no.color                = color_red;
  178.     s_confirm.no.style                = UI_LEFT;
  179.  
  180.     Menu_AddItem( &s_confirm.menu,    &s_confirm.yes );             
  181.     Menu_AddItem( &s_confirm.menu,    &s_confirm.no );
  182.  
  183.     UI_PushMenu( &s_confirm.menu );
  184.  
  185.     Menu_SetCursorToItem( &s_confirm.menu, &s_confirm.no );
  186. }
  187.